package com.duan.cart.controller;

import com.duan.cart.pojo.Cart;
import com.duan.cart.service.CartService;
import com.duan.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 新增购物车
     * @param cart
     * @return
     */
    @PostMapping("/add")
    public Result addCart(@RequestBody Cart cart) {
        try{
            cartService.addCart(cart);
        }catch (Exception e){
            return Result.fail("添加失败");
        }
        return Result.ok("添加成功");
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/findList")
    public Result<List<Cart>> selectCart(){
        List<Cart> cartList;
        try {
             cartList= cartService.selectCart();
        }catch (Exception e){
            return Result.fail("查询购物车失败");
        }
        return Result.ok("查询购物车成功",cartList);
    }

    /**
     * 修改购物车
     * @param cart
     * @return
     */
    @GetMapping("/update")
    public Result updateCart(@RequestBody Cart cart){
        try {
            cartService.updateCart(cart);
        }catch (Exception e){
            return Result.fail("修改失败");
        }
        return Result.ok("修改成功");
    }

    /**
     * 删除购物车
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result deleteCart(@RequestParam String ids){
        try {
            cartService.deleteCart(ids);
        }catch (Exception e){
            return Result.fail("删除失败");
        }
        return Result.ok("删除成功");
    }
}
